package cn.com.homeyoda.aimate.aihealth.app;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import cn.com.homeyoda.aimate.aihealth.data.Repository;
import cn.com.homeyoda.aimate.aihealth.ui.MainViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.activity.ActivityListViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.activity.NetworkTestModel;
import cn.com.homeyoda.aimate.aihealth.ui.bluetooth.BluetoothSearchModel;
import cn.com.homeyoda.aimate.aihealth.ui.bluetooth.BluetoothSuccessModel;
import cn.com.homeyoda.aimate.aihealth.ui.bluetooth_guide.BluetoothGuideViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.detail.detail.DetailViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.game.GameViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.game.play.GamePlayViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.game.record.GameRecordViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.ChronicDiseaseViewMode;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.DietPlanViewMode;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.HealthModel;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.SportPlanViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.health_manage.HealthManageViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.health_qa.HealthQaViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.home.HomeViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.kepu.KePuViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.knowledgesystem.KnowledgeSystemViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.login.LoginViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.info.MemberInfoViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageModel;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.FamilyViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.recommend.RecommendViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.problem.ProblemModel;
import cn.com.homeyoda.aimate.aihealth.ui.project.ProjectViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.protocol.ProtocolViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.questionnaire.QuestionnaireTopicViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.videoinspect.VideoInspectModel;
import cn.com.homeyoda.aimate.aihealth.ui.wechat.WeChatViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.yasee.YaseeCheckResultViewModel;

/**
 * Created By 刘纯贵
 * Created Time 2020/4/29
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final Repository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    private AppViewModelFactory(Application application, Repository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(HomeViewModel.class)) {
            return (T) new HomeViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(KnowledgeSystemViewModel.class)) {
            return (T) new KnowledgeSystemViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ProjectViewModel.class)) {
            return (T) new ProjectViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(WeChatViewModel.class)) {
            return (T) new WeChatViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MyViewModel.class)) {
            return (T) new MyViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MessageModel.class)) {
            return (T) new MessageModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ProblemModel.class)) {
            return (T) new ProblemModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(KePuViewModel.class)) {
            return (T) new KePuViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(DetailViewModel.class)) {
            return (T) new DetailViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(VideoInspectModel.class)) {
            return (T) new VideoInspectModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(QuestionnaireTopicViewModel.class)) {
            return (T) new QuestionnaireTopicViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ActivityListViewModel.class)) {
            return (T) new ActivityListViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(GameViewModel.class)) {
            return (T) new GameViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(GameRecordViewModel.class)) {
            return (T) new GameRecordViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HealthModel.class)) {
            return (T) new HealthModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(GamePlayViewModel.class)) {
            return (T) new GamePlayViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(FamilyViewModel.class)) {
            return (T) new FamilyViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HealthQaViewModel.class)) {
            return (T) new HealthQaViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(HealthManageViewModel.class)) {
            return (T) new HealthManageViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(RecommendViewModel.class)) {
            return (T) new RecommendViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(NetworkTestModel.class)) {
            return (T) new NetworkTestModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(SportPlanViewModel.class)) {
            return (T) new SportPlanViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MemberInfoViewModel.class)) {
            return (T) new MemberInfoViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(DietPlanViewMode.class)) {
            return (T) new DietPlanViewMode(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(MainViewModel.class)) {
            return (T) new MainViewModel(mApplication, mRepository);
        }else if (modelClass.isAssignableFrom(ChronicDiseaseViewMode.class)){
            return (T) new ChronicDiseaseViewMode(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(ProtocolViewModel.class)){
            return (T) new ProtocolViewModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(BluetoothGuideViewModel.class)){
            return (T) new BluetoothGuideViewModel(mApplication, mRepository);
        }else if (modelClass.isAssignableFrom(BluetoothSearchModel.class)){
            return (T) new BluetoothSearchModel(mApplication, mRepository);
        }else if (modelClass.isAssignableFrom(BluetoothSuccessModel.class)){
            return (T) new BluetoothSuccessModel(mApplication, mRepository);
        } else if (modelClass.isAssignableFrom(YaseeCheckResultViewModel.class)){
            return (T) new YaseeCheckResultViewModel(mApplication, mRepository);
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }
}
